import { _decorator, Node, Vec3 } from 'cc';
import { PlayUser } from '../core/PlayUser';

import { Databus } from '../core/Databus';
import { EightBallGame } from './EightBallGame';
import { HitBallAttribute } from '../core/Game';
import { EventManager } from '../core/EventManager';
import { BallAttribute, BallColor } from '../core/BallAttribute';

const databus = Databus.Instance();

export class FancyBallGame extends EightBallGame {
  public currentHitCount = 0;
  constructor(node: Node) {
    super(node);
    this.currentHitCount = 0;
  }
  public currentHitGanCount() { return this.currentHitCount; }
  public initBallsPosition() {
    const snookerNode = this.billiamNode.getChildByName('SnookerBalls');
    snookerNode.active = false;
    // 当前关卡存在哪些球以及位置信息
    const hurdlesInfo = databus.hurdles.getHurdlesConfig();
    const ballsNode = this.billiamNode.getChildByName('Balls');
    // 8球/9球
    ballsNode.active = true;
    const balls = ballsNode.children;
    for (const ball of balls) {
      const ba = hurdlesInfo.balls[ball.name];
      if (ba) {
        EventManager.Instance().Emit('InitBallPosition', {
          ballName: ball.name, pos: new Vec3(ba.x, 0, ba.z),
        });
      } else {
        EventManager.Instance().Emit('HideBallByName', ball.name);
      }
    }

    // 设置白球的位置
    EventManager.Instance().Emit("InitMainBallPosition", 
      new Vec3(hurdlesInfo.mainBallPosition.x, 0, hurdlesInfo.mainBallPosition.z)
    );

    this.currentHitCount = 0;
  }
  public isTargetHitBall(color: BallColor, name: string) {
    return true;
  }
  // 击打球的位置
  public defaultAimBallPosition() {
    const pos = new Vec3(0, 0, 0);
    const balls = this.getActiveBalls();
    if (balls && balls.length > 0) {
      balls[0].getWorldPosition(pos);
    }
    return pos;
  }
  // 游戏是否已经结束, 本质上没有结束，可以一直闯关
  public checkGameEnd() {
    this.currentHitCount += 1;
    const balls = this.getActiveBalls();
    const hurdlesInfo = databus.hurdles.getHurdlesConfig();
    if (balls.length <= 0) {
      EventManager.Instance().Emit('FastStartFancyGame', { win: 1 });
    } else if (this.currentHitCount >= hurdlesInfo.maxHitNum) {
      EventManager.Instance().Emit('FastEndFancyGame', { win: 0 });
    }
    return false;
  }
  // 是否进球得分
  public hasHoledScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    const balls = hitBallList;
    for (var i=balls.length-1; i>=0; i--) {
      if (balls[i].step === step) {
        if (balls[i].ba.isMainWhiteBall()) continue;
        return true;
      } else {
        break;
      }
    }
    return false;
  }
  public isIllegalHitBall(firstCollideBall: BallAttribute, hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    // 没有违规
    return { isIlleagel: false, message: 'ok' };
  }
  // 当前击球 和受保护 的球列表
  public getHitProtectBalls(color: BallColor) {
    const protectBalls = [];
    const balls = this.getActiveBalls();
    return { hitBalls: balls, protectBalls };
  }

  public getCurrentStepScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    return 0;
  }

  public getUserHitColorText(color: BallColor) {
    return color.getColorText();
  }

  public swapCurrentUserColor(hasExchangeUser: boolean) {

  }
  public isExcellentLevel(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor, userId: string) {
    return 0;
  }
  public isCurrentUserWin(side: number, isIlleagel: boolean) {
    const balls = this.getActiveBalls();
    return balls.length <= 0;
  }
  // 球桌上当前活跃的球
  public getActiveBalls() {
    const bList: Node[] = [];
    const balls = this.billiamNode.getChildByName('Balls').children;
    for (const b of balls) {
      if (b.active) {
        bList.push(b);
      }
    }
    return bList;
  }
  public penalyScore() {
    return 0;
  }
  public getAllBallList() {
    return [];
  }
  public robotRenShu(playUsers: PlayUser[]) { return false; }
}

